#
# OpenSSL/crypto/Makefile
#

ifndef KBE_ROOT
export KBE_ROOT := $(subst /kbe/src/lib/dependencies/openssl/crypto,,$(CURDIR))
endif

LIBDIR= $(KBE_ROOT)/kbe/src/libs

DIR=		crypto
TOP=		..
CC=		cc
INCLUDE=	-I. -I$(TOP) -I../include
# INCLUDES targets sudbirs!
INCLUDES=	-I.. -I../.. -I../../include
CFLAG=		-g
MAKEDEPPROG=	makedepend
MAKEDEPEND=	$(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG)
MAKEFILE=       Makefile
RM=             rm -f
AR=		ar r

RECURSIVE_MAKE=	[ -n "$(SDIRS)" ] && for i in $(SDIRS) ; do \
		    (cd $$i && echo "making $$target in $(DIR)/$$i..." && \
		    $(MAKE) -e TOP=../.. DIR=$$i INCLUDES='${INCLUDES}' $$target ) || exit 1; \
		done;

PEX_LIBS=
EX_LIBS=
 
CFLAGS= $(INCLUDE) $(CFLAG)
ASFLAGS= $(INCLUDE) $(ASFLAG)
AFLAGS=$(ASFLAGS)

LIBS=

GENERAL=Makefile README crypto-lib.com install.com

LIB= $(LIBDIR)/libcrypto.a
SHARED_LIB= libcrypto$(SHLIB_EXT)
LIBSRC=	cryptlib.c mem.c mem_clr.c mem_dbg.c cversion.c ex_data.c tmdiff.c cpt_err.c ebcdic.c uid.c o_time.c o_str.c o_dir.c
LIBOBJ= $(KBE_CONFIG)/cryptlib.o $(KBE_CONFIG)/mem.o $(KBE_CONFIG)/mem_clr.o $(KBE_CONFIG)/mem_dbg.o $(KBE_CONFIG)/cversion.o $(KBE_CONFIG)/ex_data.o $(KBE_CONFIG)/tmdiff.o $(KBE_CONFIG)/cpt_err.o $(KBE_CONFIG)/ebcdic.o $(KBE_CONFIG)/uid.o $(KBE_CONFIG)/o_time.o $(KBE_CONFIG)/o_str.o $(KBE_CONFIG)/o_dir.o $(CPUID_OBJ)

SRC= $(LIBSRC)

EXHEADER= crypto.h tmdiff.h opensslv.h opensslconf.h ebcdic.h symhacks.h \
	ossl_typ.h
HEADER=	cryptlib.h buildinf.h md32_common.h o_time.h o_str.h o_dir.h $(EXHEADER)

ALL=    $(GENERAL) $(SRC) $(HEADER)

top:
	@(cd ..; $(MAKE) DIRS=$(DIR) all)

# kbe: we don't need the shared library, only the static
#all: shared
all: buildinf.h lib subdirs

buildinf.h: ../Makefile
	( echo "#ifndef MK1MF_BUILD"; \
	echo '  /* auto-generated by crypto/Makefile for crypto/cversion.c */'; \
	echo '  #define CFLAGS "$(CC) $(CFLAG)"'; \
	echo '  #define PLATFORM "$(PLATFORM)"'; \
	echo "  #define DATE \"`LC_ALL=C LC_TIME=C date`\""; \
	echo '#endif' ) >buildinf.h

$(KBE_CONFIG)/x86cpuid-elf.s:	x86cpuid.pl perlasm/x86asm.pl
	$(PERL) x86cpuid.pl elf $(CFLAGS) $(PROCESSOR) > $@
$(KBE_CONFIG)/x86cpuid-cof.s: x86cpuid.pl perlasm/x86asm.pl
	$(PERL) x86cpuid.pl coff $(CFLAGS) $(PROCESSOR) > $@
$(KBE_CONFIG)/x86cpuid-out.s: x86cpuid.pl perlasm/x86asm.pl
	$(PERL) x86cpuid.pl a.out $(CFLAGS) $(PROCESSOR) > $@

$(KBE_CONFIG)/uplink.o:	../ms/uplink.c
	$(CC) $(CFLAGS) -c -o $@ ../ms/uplink.c

$(KBE_CONFIG)/uplink-cof.s:	../ms/uplink.pl
	$(PERL) ../ms/uplink.pl coff > $@

$(KBE_CONFIG)/x86_64cpuid.s: x86_64cpuid.pl
	$(PERL) x86_64cpuid.pl $@
$(KBE_CONFIG)/ia64cpuid.s: ia64cpuid.S
	$(CC) $(CFLAGS) -E ia64cpuid.S > $@

$(KBE_CONFIG)/%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

testapps:
	[ -z "$(THIS)" ] || (	if echo ${SDIRS} | fgrep ' des '; \
				then cd des && $(MAKE) -e des; fi )
	[ -z "$(THIS)" ] || ( cd pkcs7 && $(MAKE) -e testapps );
	@if [ -z "$(THIS)" ]; then $(MAKE) -f $(TOP)/Makefile reflect THIS=$@; fi

subdirs:
	@+target=all; $(RECURSIVE_MAKE)

files:
	$(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
	@target=files; $(RECURSIVE_MAKE)

links:
	@$(PERL) $(TOP)/util/mklink.pl ../include/openssl $(EXHEADER)
	@$(PERL) $(TOP)/util/mklink.pl ../test $(TEST)
	@$(PERL) $(TOP)/util/mklink.pl ../apps $(APPS)
	@target=links; $(RECURSIVE_MAKE)

$(KBE_CONFIG):
	@-mkdir $(KBE_CONFIG)

# lib: and $(LIB): are splitted to avoid end-less loop
lib:	$(LIB)
	@touch lib
$(LIB):	$(KBE_CONFIG) $(LIBOBJ)
	$(AR) $(LIB) $(LIBOBJ)
	$(RANLIB) $(LIB) || echo Never mind.

shared: buildinf.h lib subdirs
	if [ -n "$(SHARED_LIBS)" ]; then \
		(cd ..; $(MAKE) $(SHARED_LIB)); \
	fi

libs:
	@target=lib; $(RECURSIVE_MAKE)

install:
	@[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
	@headerlist="$(EXHEADER)"; for i in $$headerlist ;\
	do \
	(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
	chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
	done;
	@target=install; $(RECURSIVE_MAKE)

lint:
	@target=lint; $(RECURSIVE_MAKE)

depend:
	@[ -z "$(THIS)" -o -f buildinf.h ] || touch buildinf.h # fake buildinf.h if it does not exist
	@[ -z "$(THIS)" ] || $(MAKEDEPEND) -- $(CFLAG) $(INCLUDE) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
	@[ -z "$(THIS)" -o -s buildinf.h ] || rm buildinf.h
	@[ -z "$(THIS)" ] || (set -e; target=depend; $(RECURSIVE_MAKE) )
	@if [ -z "$(THIS)" ]; then $(MAKE) -f $(TOP)/Makefile reflect THIS=$@; fi

clean:
	rm -f buildinf.h $(KBE_CONFIG)/*.s *.s $(KBE_CONFIG)/*.o *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
	@+target=clean; $(RECURSIVE_MAKE)

dclean:
	$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
	mv -f Makefile.new $(MAKEFILE)
	@target=dclean; $(RECURSIVE_MAKE)

# DO NOT DELETE THIS LINE -- make depend depends on it.

$(KBE_CONFIG)/cpt_err.o: ../include/openssl/bio.h ../include/openssl/crypto.h
$(KBE_CONFIG)/cpt_err.o: ../include/openssl/e_os2.h ../include/openssl/err.h
$(KBE_CONFIG)/cpt_err.o: ../include/openssl/lhash.h ../include/openssl/opensslconf.h
$(KBE_CONFIG)/cpt_err.o: ../include/openssl/opensslv.h ../include/openssl/ossl_typ.h
$(KBE_CONFIG)/cpt_err.o: ../include/openssl/safestack.h ../include/openssl/stack.h
$(KBE_CONFIG)/cpt_err.o: ../include/openssl/symhacks.h cpt_err.c
$(KBE_CONFIG)/cryptlib.o: ../e_os.h ../include/openssl/bio.h ../include/openssl/buffer.h
$(KBE_CONFIG)/cryptlib.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/cryptlib.o: ../include/openssl/err.h ../include/openssl/lhash.h
$(KBE_CONFIG)/cryptlib.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/cryptlib.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/cryptlib.o: ../include/openssl/stack.h ../include/openssl/symhacks.h cryptlib.c
$(KBE_CONFIG)/cryptlib.o: cryptlib.h
$(KBE_CONFIG)/cversion.o: ../e_os.h ../include/openssl/bio.h ../include/openssl/buffer.h
$(KBE_CONFIG)/cversion.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/cversion.o: ../include/openssl/err.h ../include/openssl/lhash.h
$(KBE_CONFIG)/cversion.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/cversion.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/cversion.o: ../include/openssl/stack.h ../include/openssl/symhacks.h buildinf.h
$(KBE_CONFIG)/cversion.o: cryptlib.h cversion.c
$(KBE_CONFIG)/ebcdic.o: ../include/openssl/e_os2.h ../include/openssl/opensslconf.h ebcdic.c
$(KBE_CONFIG)/ex_data.o: ../e_os.h ../include/openssl/bio.h ../include/openssl/buffer.h
$(KBE_CONFIG)/ex_data.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/ex_data.o: ../include/openssl/err.h ../include/openssl/lhash.h
$(KBE_CONFIG)/ex_data.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/ex_data.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/ex_data.o: ../include/openssl/stack.h ../include/openssl/symhacks.h cryptlib.h
$(KBE_CONFIG)/ex_data.o: ex_data.c
$(KBE_CONFIG)/mem.o: ../e_os.h ../include/openssl/bio.h ../include/openssl/buffer.h
$(KBE_CONFIG)/mem.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/mem.o: ../include/openssl/err.h ../include/openssl/lhash.h
$(KBE_CONFIG)/mem.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/mem.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/mem.o: ../include/openssl/stack.h ../include/openssl/symhacks.h cryptlib.h
$(KBE_CONFIG)/mem.o: mem.c
$(KBE_CONFIG)/mem_clr.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/mem_clr.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/mem_clr.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/mem_clr.o: ../include/openssl/stack.h ../include/openssl/symhacks.h mem_clr.c
$(KBE_CONFIG)/mem_dbg.o: ../e_os.h ../include/openssl/bio.h ../include/openssl/buffer.h
$(KBE_CONFIG)/mem_dbg.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/mem_dbg.o: ../include/openssl/err.h ../include/openssl/lhash.h
$(KBE_CONFIG)/mem_dbg.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/mem_dbg.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/mem_dbg.o: ../include/openssl/stack.h ../include/openssl/symhacks.h cryptlib.h
$(KBE_CONFIG)/mem_dbg.o: mem_dbg.c
$(KBE_CONFIG)/o_dir.o: ../e_os.h ../include/openssl/e_os2.h ../include/openssl/opensslconf.h
$(KBE_CONFIG)/o_dir.o: LPdir_unix.c o_dir.c o_dir.h
$(KBE_CONFIG)/o_str.o: ../e_os.h ../include/openssl/e_os2.h ../include/openssl/opensslconf.h
$(KBE_CONFIG)/o_str.o: o_str.c o_str.h
$(KBE_CONFIG)/o_time.o: ../include/openssl/e_os2.h ../include/openssl/opensslconf.h o_time.c
$(KBE_CONFIG)/o_time.o: o_time.h
$(KBE_CONFIG)/tmdiff.o: ../e_os.h ../include/openssl/bio.h ../include/openssl/buffer.h
$(KBE_CONFIG)/tmdiff.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/tmdiff.o: ../include/openssl/err.h ../include/openssl/lhash.h
$(KBE_CONFIG)/tmdiff.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/tmdiff.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/tmdiff.o: ../include/openssl/stack.h ../include/openssl/symhacks.h
$(KBE_CONFIG)/tmdiff.o: ../include/openssl/tmdiff.h cryptlib.h tmdiff.c
$(KBE_CONFIG)/uid.o: ../include/openssl/crypto.h ../include/openssl/e_os2.h
$(KBE_CONFIG)/uid.o: ../include/openssl/opensslconf.h ../include/openssl/opensslv.h
$(KBE_CONFIG)/uid.o: ../include/openssl/ossl_typ.h ../include/openssl/safestack.h
$(KBE_CONFIG)/uid.o: ../include/openssl/stack.h ../include/openssl/symhacks.h uid.c
